<?php

namespace module\index\validate;

use app\Validate;

class Visitor extends Validate
{
    /**
     * 验证场景.
     *
     * @var array<array<string>>
     */
    protected $scene = [
        'login' => ['mobile', 'openid', 'nickname', 'avatar'],  // 密码登录
        'sms' => ['mobile', 'smscode'], // 短信登录
        'signup' => ['mobile', 'smscode', 'password', 'password_confirm'],
        'forget' => ['mobile', 'smscode', 'password', 'password_confirm'],
        'bind' => ['mobile', 'smscode', 'openid'],  // 密码登录
        'wxlogin' => ['openid'],
    ];
    /**
     * 验证规则.
     *
     * @var array<string>
     */
    protected $rule = [
        'mobile' => 'require|regex:/^1[35789]\d{9}$/i|unique:user|exists:user',  // 手机号
        'nickname'  => 'max:40',  // 迷城
        'avatar'  => 'max:160',  // 头像
        'password'  => 'require|min:6|max:16',  // 密码
        'smscode'  => 'require|length:6|smscode',  // 短信动态码
        'captcha'  => 'require',  // 密码
        'password_confirm' => 'require|confirm:password',  // 确认密码
        'openid' => 'require|checkOpenid',
    ];
    /**
     * 验证字段描述
     * 
     * @var array<string>
     */
    protected $field = [
        'mobile' => '账号',
        'password' => '密码',
        'smscode' => '短信验证码',
        'captcha' => '图像验证码',
        'password_confirm' => '确认密码',
    ];
    // 自定义验证规则
    protected function checkOpenid($value, $rule, $data=[])
    {
        return true;
    }
    /**
     * 验证信息描述
     * 
     * @var array<string>
     */
    protected $message = [
        'mobile.regex' => '手机号格式错误',
    ];
    // login 验证场景定义
    public function sceneLogin()
    {
    	return $this->only($this->scene['login'])->remove('mobile', 'unique|exists');
    }    
    // signup 验证场景定义
    public function sceneSignup()
    {
    	return $this->only($this->scene['signup'])->remove('mobile', 'exists');
    }    
    // forget 验证场景定义
    public function sceneForget()
    {
    	return $this->only($this->scene['forget'])->remove('mobile', 'unique');
    }    
    // bind 验证场景定义
    public function sceneBind()
    {
    	return $this->only($this->scene['forget'])->remove('mobile', 'exists|unique');
    }    
}